﻿using System;
using System.Collections.Generic;
using System.Text;
using ElteIk.RoboSoccer.Core;
using ElteIk.RoboSoccer.Core.Physics;

namespace ElteIk.RoboSoccer.DavidTactics
{
    class PathfindingAction : IAction
    {
        private RobotPlayer robot;
        private Vector2D destination;
        private ActionList movings;

        public PathfindingAction(RobotPlayer robot, Vector2D destination)
        {
            this.robot = robot;
            this.destination = destination;
            movings = new ActionList();
            Recalculate();
        }

        public void Do()
        {
            movings.Do();
        }

        public bool HasEnded()
        {
            return movings.HasEnded();
        }

        private void Recalculate()
        {
            movings.Clear();
            Route route = DavidTactics.PathFinder.Search(robot.Position, destination, true);
            for (int i = 0; i < route.Count-1; ++i)
                movings.Add(new MovingAction(robot, route[i], route[i+1]));
            movings.Add(new MovingAction(robot, route[route.Count - 1], route[route.Count - 1]));
        }
    }
}